
// <copyright file="DataReaderBase.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The data reader base.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Data
{
    using System;
    using System.Data;

    /// <summary>
    /// The data reader base.
    /// </summary>
    public abstract class DataReaderBase : AbstractDisposable
    {
        #region Fields

        /// <summary>
        /// The adaptedObject.
        /// </summary>
        private readonly IDataReader adaptedObject;

        #endregion Fields

        #region Constructors and Finalizers

        /// <summary>
        /// Initializes a new instance of the <see cref="DataReaderBase"/> class.
        /// </summary>
        /// <param name="adaptedObject">
        /// The adaptedObject.
        /// </param>
        protected DataReaderBase(IDataReader adaptedObject)
        {
            this.adaptedObject = adaptedObject;
        }

        #endregion Constructors and Finalizers

        #region Public Properties

        /// <summary>
        /// Gets the depth.
        /// </summary>
        public virtual int Depth
        {
            get
            {
                return this.adaptedObject.Depth;
            }
        }

        /// <summary>
        /// Gets the field count.
        /// </summary>
        public virtual int FieldCount
        {
            get
            {
                return this.adaptedObject.FieldCount;
            }
        }

        /// <summary>
        /// Gets a value indicating whether is closed.
        /// </summary>
        public virtual bool IsClosed
        {
            get
            {
                return this.adaptedObject.IsClosed;
            }
        }

        /// <summary>
        /// Gets the number of records read so far.
        /// </summary>
        public long NumberOfRecordsReadSoFar { get; private set; }

        /// <summary>
        /// Gets the records affected.
        /// </summary>
        public virtual int RecordsAffected
        {
            get
            {
                return this.adaptedObject.RecordsAffected;
            }
        }

        #endregion Public Properties

        #region Properties

        /// <summary>
        /// Gets the adaptedObject.
        /// </summary>
        protected IDataReader AdaptedObject
        {
            get
            {
                return this.adaptedObject;
            }
        }

        #endregion Properties

        #region Public Methods and Operators

        /// <summary>
        /// The close.
        /// </summary>
        public virtual void Close()
        {
            this.adaptedObject.Close();
        }

        /// <summary>
        /// The get byte.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="byte"/>.
        /// </returns>
        public virtual byte GetByte(int i)
        {
            return this.adaptedObject.GetByte(i);
        }

        /// <summary>
        /// The get bytes.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <param name="fieldOffset">
        /// The field offset.
        /// </param>
        /// <param name="buffer">
        /// The buffer.
        /// </param>
        /// <param name="bufferOffset">
        /// The bufferOffset.
        /// </param>
        /// <param name="length">
        /// The length.
        /// </param>
        /// <returns>
        /// The <see cref="long"/>.
        /// </returns>
        public virtual long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferOffset, int length)
        {
            return this.adaptedObject.GetBytes(i, fieldOffset, buffer, bufferOffset, length);
        }

        /// <summary>
        /// The get chars.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <param name="fieldOffset">
        /// The fieldOffset.
        /// </param>
        /// <param name="buffer">
        /// The buffer.
        /// </param>
        /// <param name="bufferOffset">
        /// The bufferOffset.
        /// </param>
        /// <param name="length">
        /// The length.
        /// </param>
        /// <returns>
        /// The <see cref="long"/>.
        /// </returns>
        public virtual long GetChars(int i, long fieldOffset, char[] buffer, int bufferOffset, int length)
        {
            return this.adaptedObject.GetChars(i, fieldOffset, buffer, bufferOffset, length);
        }

        /// <summary>
        /// The get data.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="IDataReader"/>.
        /// </returns>
        public virtual IDataReader GetData(int i)
        {
            return this.adaptedObject.GetData(i);
        }

        /// <summary>
        /// The get data type name.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public virtual string GetDataTypeName(int i)
        {
            return this.adaptedObject.GetDataTypeName(i);
        }

        /// <summary>
        /// The get field type.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="Type"/>.
        /// </returns>
        public virtual Type GetFieldType(int i)
        {
            return this.adaptedObject.GetFieldType(i);
        }

        /// <summary>
        /// The get name.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        public virtual string GetName(int i)
        {
            return this.adaptedObject.GetName(i);
        }

        /// <summary>
        /// The get ordinal.
        /// </summary>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public virtual int GetOrdinal(string name)
        {
            return this.adaptedObject.GetOrdinal(name);
        }

        /// <summary>
        /// The get schema table.
        /// </summary>
        /// <returns>
        /// The <see cref="DataTable"/>.
        /// </returns>
        public virtual DataTable GetSchemaTable()
        {
            return this.adaptedObject.GetSchemaTable();
        }

        /// <summary>
        /// The is db null.
        /// </summary>
        /// <param name="i">
        /// The i.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public virtual bool IsDBNull(int i)
        {
            return this.adaptedObject.IsDBNull(i);
        }

        /// <summary>
        /// The next result.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public virtual bool NextResult()
        {
            return this.adaptedObject.NextResult();
        }

        /// <summary>
        /// The read.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool Read()
        {
            bool couldRead = this.readProtected();
            if (couldRead)
            {
                this.NumberOfRecordsReadSoFar++;
            }

            return couldRead;
        }

        #endregion Public Methods and Operators

        #region Methods

        /// <summary>
        /// The dispose.
        /// </summary>
        /// <param name="disposing">
        /// The disposing.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            this.adaptedObject.Dispose();
        }

        /// <summary>
        /// The read protected.
        /// </summary>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        protected virtual bool readProtected()
        {
            return this.adaptedObject.Read();
        }

        #endregion Methods
    }
}